//
//  WHYNewFeatureViewController.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/25.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

private let WHYWBNewFeatureViewCellID = "WHYWBNewFeatureViewCell"

private let WHYWBNewFeatureCount = 4

class WHYNewFeatureViewController: UICollectionViewController {

//    MARK: - 构造函数
    init() {
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//     MARK: - 视图生命周期函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
        
    }
    
    private func prepareCollectionView () {
        
        collectionView?.backgroundColor = UIColor.whiteColor()
        collectionView?.registerClass(WHYNewFeatureCell.self, forCellWithReuseIdentifier: WHYWBNewFeatureViewCellID)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

}

// MARK: - 数据源和代理函数
extension WHYNewFeatureViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WHYWBNewFeatureCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WHYWBNewFeatureViewCellID, forIndexPath: indexPath) as! WHYNewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.size.width) // 1, 2, 3
        
        if page != WHYWBNewFeatureCount - 1 {
            return
        }
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as? WHYNewFeatureCell
        cell?.showStartButton()
    }
    
}

// MARK: - 自定义cell
private class WHYNewFeatureCell: UICollectionViewCell {
    
    var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
    @objc private func clickStartButton() {
        NSNotificationCenter.defaultCenter().postNotificationName(WHYWBSwitchViewControllerNotification, object: nil)
    }
    
    private func showStartButton () {
        startButton.hidden = false
        startButton.transform  = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.5,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }

//    MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - 设置UI布局
    private func setupUI () {
        iconView.frame = bounds
        addSubview(iconView)
        
        addSubview(startButton)
        startButton.hidden = true
        startButton.layer.cornerRadius = 5
        startButton.layer.masksToBounds = true
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.bottom.equalTo(snp_bottom).multipliedBy(0.7)
        }
        
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
    }
    
//    MARK: - 懒加载子控件
    private lazy var iconView = UIImageView()
    
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backColor: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
    
}
